//
//  NTianBaoDetailViewController.m
//  nncloud
//
//  Created by 朵 on 2018/11/29.
//  Copyright © 2018年 macfai. All rights reserved.
//

#import "NTianBaoDetailViewController.h"
#import "NZhaopinCell.h"
#import "NMiaoshuCell.h"
#import "NTianbaoSubVC.h"
#import "NIndustyBaobiaoModel.h"
#import "NTianbaoSubVC.h"

@interface NTianBaoDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,strong)NSArray *infoArray;


///填报情况描述
@property (nonatomic,copy)NSString *content;

///工业报表数据id
@property (nonatomic,copy)NSString *IndusId;

@end

@implementation NTianBaoDetailViewController

#define cellIdentifier @"zhaopin"
#define miaoshuCell @"miaoshu"
#define SectionHeaderHeight  10.f
#define  topHeaderHeight  50.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , topHeaderHeight, ScreenWidth , ScreenHeight - 64-topHeaderHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.rowHeight = 115.f;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NZhaopinCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"NMiaoshuCell" bundle:nil] forCellReuseIdentifier:miaoshuCell];
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    if (_atitle) {
        
        //工业报表详情  企业报表详情
        [self.navigationItem setItemWithTitle:_atitle textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }
    
    if ([_atitle containsString:@"财务"]) {
        [NUserModel shareInstance].isIndustryOrFinance = @"财务报表";
    }else{
        [NUserModel shareInstance].isIndustryOrFinance = @"工业报表";
    }
    [self addQiyeTitle];
    self.dataArray = [NSMutableArray array];
    self.titleArray = @[@"联系人",@"单位负责人",@"统计负责人",@"填报人",@"手机号码",];
//    self.infoArray = @[@"",@"王继坤",@"六三",@"张思",@"16088888888",];
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)addQiyeTitle{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25, 0, ScreenWidth, topHeaderHeight)];
//    label.backgroundColor = kMagentaColor;
    label.text = self.qiyeName?:@"";
    label.font = kFont16;
    label.textColor = kDarkGrayColor;
    [self.view addSubview:label];
    
}

- (void)loadData{
    
    NSDictionary *dic = @{@"id":self.baobiaoId};
    //后期逐渐添加参数目前不需要参数
//    NSDictionary *dic = @{@"userId":[NUserModel shareInstance].userInfoId};
    
    NSString *url;
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        url = kIndustryBaobiaoDetailUrl;
    }else{
        url = kCaiwuBaobiaoDetailUrl;
    }
    [[NNRCNetRequest manager]getWithUrl:url params:dic success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {

            NSDictionary *dict = result[@"page"];
//            YYLog(@"%@",arr);
            
//            for (NSDictionary *dict in arr) {
//                NIndustyBaobiaoModel *model = [NIndustyBaobiaoModel mj_objectWithKeyValues:dict];
//                [ws.dataArray addObject:model];
//            }
            [ws formDataWithModel:dict];
            
            if ([ws.atitle containsString:@"财务"]) {
                self.IndusId = dict[@"financeId"];
            }else{
                self.IndusId = dict[@"industryId"];
            }
            
            self.content = @"";
            
            if ([ws.atitle isEqualToString:@"工业报表详情"]) {
                
                self.content = dict[@"industryExplain"];
                
            }else{
                
                self.content = dict[@"financeExplain"];
                
            }
            
            [ws.tableView reloadData];

        }else{
            [AFToast showText:result[@"msg"]];
        }
        
    } failure:^(id failure) {
        
    }];
}

- (void)formDataWithModel:(NSDictionary  *)dict{
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        NSString *fuzeren = dict[@"industryCompany"];
        NSString *tongji = dict[@"industryStatistics"];
        NSString *tianbao = dict[@"industryApplicant"];
        NSString *tel = dict[@"industryPhone"];
        self.infoArray = @[@"",fuzeren,tongji,tianbao,tel];
    }else{
        NSString *fuzeren = dict[@"financeCompany"];
        NSString *tongji = dict[@"financeStatistics"];
        NSString *tianbao = dict[@"financeApplicant"];
        NSString *tel = dict[@"financePhone"];
        self.infoArray = @[@"",fuzeren,tongji,tianbao,tel];
    }
    
}

#pragma mark - delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NTianbaoSubVC *sub = [NTianbaoSubVC new];
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        
        switch (indexPath.section) {
            case 0:
            {
                sub.atitle = @"工业总产值";
                sub.type = @"1";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 1:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"工业生产总值（当年价格）";
                sub.type = @"2";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 2:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"主要工业品产量";
                sub.type = @"3";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 3:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"补充资料";
                sub.type = @"4";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
                
            default:
                break;
        }
        
        if (self.IndusId) {
            sub.IndustryId = self.IndusId;
        }
         [self.navigationController pushViewController:sub animated:YES];
        
    }else{
        
        switch (indexPath.section) {
            case 0:
            {
                sub.atitle = @"年初存货";
                sub.type = @"1";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 1:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"期末资产负债";
                sub.type = @"2";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 2:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"损益及分配";
                sub.type = @"3";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 3:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"应交增值税";
                sub.type = @"4";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
            case 4:
            {
//                NTianbaoSubVC *sub = [NTianbaoSubVC new];
                sub.atitle = @"平均用工人数";
                sub.type = @"5";
//                [self.navigationController pushViewController:sub animated:YES];
            }
                break;
                
            default:
                break;
        }
        if (self.IndusId) {
            sub.IndustryId = self.IndusId;
        }
         [self.navigationController pushViewController:sub animated:YES];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        if (indexPath.section==4) {
            return 290.f;
        }else{
            return 50.f;
        }
    }else{
        if (indexPath.section==5) {
            return 190.f;
        }else{
            return 50.f;
        }
    }
    return 0.f;
}
#pragma mark - datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        return 6;
    }else{
        return 7;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        
        if (section==5) {
            return  self.titleArray.count;
        }else{
            return 1;
        }
    }else{
        if (section==6) {
            return  self.titleArray.count;
        }else{
            return 1;
        }
        
        
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_atitle isEqualToString:@"工业报表详情"]) {
        
        if (indexPath.section==0) {
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
            cell.img.hidden = false;
            cell.name.text = @"一、工业总产值";
            cell.nameLeftMargin.constant = 25.f;
            return cell;
            
        }else if(indexPath.section==1){
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
             cell.img.hidden = false;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
            cell.name.text = @"二、工业生产总值按照工业行业小类分";
            return cell;
            
        }else if (indexPath.section==2){
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
             cell.img.hidden = false;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
            cell.name.text = @"三、主要工业产品产量";
            return cell;
            
        }
        else if (indexPath.section==3){
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
             cell.img.hidden = false;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
            cell.name.text = @"四、补充资料";
            return cell;
        }
        else if (indexPath.section==4){
            NMiaoshuCell *cell = [tableView dequeueReusableCellWithIdentifier:miaoshuCell forIndexPath:indexPath];
            cell.nameLabel.text = @"填报情况描述";
//            cell.detailLable.text = @"说明：1.统计范围：辖区内规模以上工业法人单位。2.报送日期及方式：调查单位2、5、10月月后5日，6、7月月后6日，3、4、8、11、12月月后7日，9月月后9日12:00前独立自行网上填报，1月免报；省级统计机构2、5、10月月后7日，6、7月月后8日，3、4、8、11、12月月后9日，9月月后11日18:00前完成数据审核、验收、上报，1月免报";
            cell.detailLable.text = self.content;
            return cell;
        }
        else{
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = false;
            cell.name.text = self.titleArray[indexPath.row];
            cell.salary.text = self.infoArray[indexPath.row];
            cell.img.hidden = YES;
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 49.5, ScreenWidth, 0.5)];
            line.backgroundColor = kLightGrayColor;
            [cell addSubview:line];
            return cell;
        }
    }else{
        ///财务填报详情
        if (indexPath.section==0) {
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
             cell.img.hidden = false;
            cell.name.text = @"一、年初存货";
            cell.nameLeftMargin.constant = 25.f;
            return cell;
            
        }else if(indexPath.section==1){
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
             cell.img.hidden = false;
            cell.salary.hidden = YES;
            cell.name.text = @"二、期末资产负债";
            //        cell.name.text = self.titleArray[indexPath.row];
            //        cell.salary.text = self.infoArray[indexPath.row];
            return cell;
            
        }else if (indexPath.section==2){
            
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
             cell.img.hidden = false;
            cell.salary.hidden = YES;
            cell.name.text = @"三、损益及分配";
            return cell;
            
        }
        else if (indexPath.section==3){
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
             cell.img.hidden = false;
            cell.name.text = @"四、应交增值税";
            return cell;
        }
        else if (indexPath.section==4){
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = YES;
             cell.img.hidden = false;
            cell.name.text = @"五、平均用工人数";
            return cell;
        }
        else if (indexPath.section==5){
            NMiaoshuCell *cell = [tableView dequeueReusableCellWithIdentifier:miaoshuCell forIndexPath:indexPath];
            cell.nameLabel.text = @"填报情况描述";
            cell.detailLable.text = @"说明：1.统计范围：辖区内规模以上工业法人单位。2.报送日期及方式：调查单位2、5、10月月后5日，6、7月月后6日，3、4、8、11、12月月后7日，9月月后9日12:00前独立自行网上填报，1月免报；省级统计机构2、5、10月月后7日，6、7月月后8日，3、4、8、11、12月月后9日，9月月后11日18:00前完成数据审核、验收、上报，1月免报";
            return cell;
        }
        else{
            NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.nameLeftMargin.constant = 25.f;
            cell.area.hidden = YES;
            cell.date.hidden = YES;
            cell.company.hidden = YES;
            cell.salary.hidden = false;
            cell.name.text = self.titleArray[indexPath.row];
            cell.salary.text = self.infoArray[indexPath.row];
            cell.img.hidden = YES;
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 49.5, ScreenWidth, 0.5)];
            line.backgroundColor = kLightGrayColor;
            [cell addSubview:line];
            return cell;
        }
    }
   
    return nil;
}

//根据字符串内容返回字符串的size
- (CGSize)sizeWithLabelWithContent:(NSString *)content width:(CGFloat)width font:(UIFont *)font{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[content boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, sizeHieght);
}

#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}





@end
